package org.xxpay.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.common.util.StrUtil;
import org.xxpay.core.entity.*;
import org.xxpay.core.service.IAgentInfoService;
import org.xxpay.core.service.IChannelMchInfoService;
import org.xxpay.core.service.ISysConfigService;
import org.xxpay.service.dao.mapper.AgentAccountMapper;
import org.xxpay.service.dao.mapper.AgentInfoMapper;
import org.xxpay.service.dao.mapper.ChannelMchInfoMapper;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: dingzhiwei
 * @date: 2018/4/27
 * @description:
 */
@Service(interfaceName = "org.xxpay.core.service.IChannelMchInfoService", version = "1.0.0", retries = -1)
public class ChannelMchInfoServiceImpl implements IChannelMchInfoService {

    @Autowired
    private ChannelMchInfoMapper channelMchInfoMapper;


    @Override
    public Integer count(ChannelMchInfo channelMchInfo) {
        ChannelMchInfoExample example = new ChannelMchInfoExample();
        ChannelMchInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelMchInfo);
        return channelMchInfoMapper.countByExample(example);
    }

    @Override
    public List<ChannelMchInfo> select(int offset, int limit, ChannelMchInfo channelMchInfo) {
        ChannelMchInfoExample example = new ChannelMchInfoExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        ChannelMchInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelMchInfo);
        return channelMchInfoMapper.selectByExample(example);
    }
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    @Override
    public int add(ChannelMchInfo channelMchInfo) {
        // 插入代理商基本信息
       int i =  channelMchInfoMapper.insertSelective(channelMchInfo);
        // 插入代理商账户信息
//        ChannelMchInfo channelMchInfo = new ChannelMchInfo();
//        agentAccount.setAgentId(agentInfo.getAgentId());
//        agentAccount.setBalance(BigDecimal.ZERO.longValue());
//        agentAccount.setSecurityMoney(BigDecimal.ZERO.longValue());
//        agentAccount.setSettAmount(BigDecimal.ZERO.longValue());
//        agentAccount.setUnBalance(BigDecimal.ZERO.longValue());
//        agentAccount.setTodayExpend(BigDecimal.ZERO.longValue());
//        agentAccount.setTodayIncome(BigDecimal.ZERO.longValue());
//        agentAccount.setTotalExpend(BigDecimal.ZERO.longValue());
//        agentAccount.setTotalIncome(BigDecimal.ZERO.longValue());
//        return agentAccountMapper.insertSelective(agentAccount);
        return i;
    }
//
    @Override
    public int update(ChannelMchInfo channelMchInfo) {
        return channelMchInfoMapper.updateByPrimaryKeySelective(channelMchInfo);
    }
//
    @Override
    public ChannelMchInfo find(ChannelMchInfo channelMchInfo) {
        ChannelMchInfoExample example = new ChannelMchInfoExample();
        ChannelMchInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelMchInfo);
        List<ChannelMchInfo> channelMchInfoList = channelMchInfoMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(channelMchInfoList)) return null;
        return channelMchInfoList.get(0);
    }

    @Override
    public ChannelMchInfo findBychannelMchId(Long channelMchId) {
        return channelMchInfoMapper.selectByPrimaryKey(channelMchId);
    }

    @Override
    public int batchDeleteChannelMch(List<Long> channelMchIds) {
        if(CollectionUtils.isEmpty(channelMchIds)) return 0;
        int deleteCount = 0;
        for(Long channelMchId : channelMchIds) {
            deleteCount = deleteCount + channelMchInfoMapper.deleteByPrimaryKey(channelMchId);
        }
        return deleteCount;
    }
    //
//    @Override
//    public AgentInfo findByLoginName(String loginName) {
//        if(StringUtils.isBlank(loginName)) return null;
//        AgentInfo agentInfo;
//        if(StrUtil.checkEmail(loginName)) {
//            agentInfo = findByEmail(loginName);
//            if(agentInfo != null) return agentInfo;
//        }
//        if(StrUtil.checkMobileNumber(loginName)) {
//            agentInfo = findByMobile(Long.parseLong(loginName));
//            if(agentInfo != null) return agentInfo;
//        }
//        if(NumberUtils.isDigits(loginName)) {
//            agentInfo = findByAgentId(Long.parseLong(loginName));
//            if(agentInfo != null) return agentInfo;
//        }
//        agentInfo = findByUserName(loginName);
//        return agentInfo;
//    }
//

//
    @Override
    public ChannelMchInfo findByUserName(String userName) {
        System.out.println("========"+userName);
        ChannelMchInfo channelMchInfo = new ChannelMchInfo();
        channelMchInfo.setUserName(userName);
        return find(channelMchInfo);
    }
//
    @Override
    public ChannelMchInfo findByMobile(Long mobile) {
        ChannelMchInfo channelMchInfo = new ChannelMchInfo();
        channelMchInfo.setMobile(mobile);
        return find(channelMchInfo);
    }

    @Override
    public ChannelMchInfo findByEmail(String email) {
        ChannelMchInfo channelMchInfo = new ChannelMchInfo();
        channelMchInfo.setEmail(email);
        return find(channelMchInfo);
    }
//
//    @Override
//    public AgentInfo findByParentAgentId(Long parentAgentId) {
//        AgentInfo agentInfo = new AgentInfo();
//        agentInfo.setParentAgentId(parentAgentId);
//        return find(agentInfo);
//    }
//

//
//
//
//    @Override
//    public Map count4Agent() {
//        Map param = new HashMap<>();
//        return agentInfoMapper.count4Agent(param);
//    }
//
//    @Override
//    public AgentInfo reBuildAgentInfoSettConfig(AgentInfo info) {
//        // 继承系统,按系统配置设置结算属性
//        if(info.getSettConfigMode() == 1) {
//            JSONObject settObj = sysConfigService.getSysConfigObj("sett");
//            info.setDrawFlag(settObj.getByte("drawFlag"));
//            info.setAllowDrawWeekDay(settObj.getString("allowDrawWeekDay"));
//            info.setDrawDayStartTime(settObj.getString("drawDayStartTime"));
//            info.setDrawDayEndTime(settObj.getString("drawDayEndTime"));
//            info.setDayDrawTimes(settObj.getInteger("dayDrawTimes"));
//            info.setDrawMaxDayAmount(settObj.getLong("drawMaxDayAmount"));
//            info.setMaxDrawAmount(settObj.getLong("maxDrawAmount"));
//            info.setMinDrawAmount(settObj.getLong("minDrawAmount"));
//            info.setFeeType(settObj.getByte("feeType"));
//            info.setFeeRate(settObj.getBigDecimal("feeRate"));
//            info.setFeeLevel(settObj.getString("feeLevel"));
//            info.setDrawFeeLimit(settObj.getLong("drawFeeLimit"));
//            info.setSettMode(settObj.getByte("settMode"));
//        }
//        return info;
//    }
//
//    @Override
//    public int getRiskDay(Long agentId) {
//        AgentInfo agentInfo = findByAgentId(agentId);
//        if(agentInfo == null) return 1;
//        agentInfo = reBuildAgentInfoSettConfig(agentInfo);
//        if(agentInfo.getSettMode() == 1) {
//            return 0;
//        }
//        return 1;
//    }
//
//    @Override
//    public List<AgentInfo> selectAll(AgentInfo agentInfo) {
//        AgentInfoExample example = new AgentInfoExample();
//        example.setOrderByClause("agentLevel ASC");
//        AgentInfoExample.Criteria criteria = example.createCriteria();
//        setCriteria(criteria,agentInfo);
//        return agentInfoMapper.selectByExample(example);
//    }
//
//    /**
//     * 查询总代理下的二级代理列表
//     */
//    @Override
//    public List<Map> selectInfoAndAccount(int page, int pageSize, AgentInfo agentInfo) {
//        Map param = new HashMap();
//        param.put("offset", page);
//        param.put("limit", pageSize);
//        if (agentInfo.getParentAgentId() !=null && agentInfo.getParentAgentId() != 0) param.put("parentAgentId", agentInfo.getParentAgentId());
//        return agentInfoMapper.selectInfoAndAccount(param);
//    }
//
    void setCriteria(ChannelMchInfoExample.Criteria criteria, ChannelMchInfo channelMchInfo) {
        if(channelMchInfo != null) {
            if(channelMchInfo.getChannelMchId() != null) criteria.andChannelMchIdEqualTo(channelMchInfo.getChannelMchId() );
            if(channelMchInfo.getSupChannelMchAgentId() != null) criteria.andSupChannelMchAgentIdEqualTo(channelMchInfo.getSupChannelMchAgentId());
            if(channelMchInfo.getEmail() != null) criteria.andEmailEqualTo(channelMchInfo.getEmail());
            if(channelMchInfo.getMobile() != null) criteria.andMobileEqualTo(channelMchInfo.getMobile());
            if(channelMchInfo.getUserName() != null) criteria.andUserNameEqualTo(channelMchInfo.getUserName());
            if(channelMchInfo.getStatus() != null && channelMchInfo.getStatus().byteValue() != -99) criteria.andStatusEqualTo(channelMchInfo.getStatus());
            if(channelMchInfo.getChannelMchAgentLevel() != null) criteria.andChannelMchAgentLevelEqualTo(channelMchInfo.getChannelMchAgentLevel());
        }
    }
}
